//
//  OosElements.m
//  com.oos.kraken
//
//  Created by Pablo Gonzalez on 2/14/11.
//  Copyright 2011 11870.com. All rights reserved.
//

#import "OosElements.h"
#import "../../Constants/OosNamespaces.h"

// Extensions

@implementation OosValueElementExtension
+ (NSString *) extensionElementURI			{ return OOS_NAMESPACE; }
+ (NSString *) extensionElementPrefix		{ return OOS_PREFIX; }
+ (NSString *) extensionElementLocalName	{ return nil; }
@end

@implementation OosObjectExtension
+ (NSString *) extensionElementURI			{ return OOS_NAMESPACE; }
+ (NSString *) extensionElementPrefix		{ return OOS_PREFIX; }
+ (NSString *) extensionElementLocalName	{ return nil; }
@end

// Simple Elements

@implementation OosUserAddress
+ (NSString *) extensionElementLocalName	{ return @"useraddress"; }
@end

@implementation OosUrl
+ (NSString *) extensionElementLocalName	{ return @"url"; }
@end

@implementation OosId
+ (NSString *) extensionElementLocalName	{ return @"id"; }
@end

@implementation OosTelephone
+ (NSString *) extensionElementLocalName	{ return @"telephone"; }
@end

@implementation OosSlug
+ (NSString *) extensionElementLocalName	{ return @"slug"; }
@end

@implementation OosReviewCounter
+ (NSString *) extensionElementLocalName	{ return @"reviews-counter"; }
@end

@implementation OosSavedCounter
+ (NSString *) extensionElementLocalName	{ return @"saved-counter"; }
@end

@implementation OosFnv
+ (NSString *) extensionElementLocalName	{ return @"fnv"; }
@end

@implementation OosAvatar
+ (NSString *) extensionElementLocalName	{ return @"avatar"; }
@end


static NSString *ratingValueKey = @"value";

@implementation OosRating
+ (NSString *) extensionElementLocalName	{ return @"rating"; }

- (void) addParseDeclarations
{
	NSArray *attributes = [NSArray arrayWithObject:ratingValueKey];
	[self addLocalAttributeDeclarations:attributes];
	[self addContentValueDeclaration]; 
}

- (NSNumber *) numberValue 
{
	NSString *stringValue = [self stringValueForAttribute:ratingValueKey];
	return [NSNumber numberWithInt:[stringValue intValue]];
}

- (void) setNumberValue:(NSNumber *)value 
{
	[self setStringValue:[value stringValue] forAttribute:ratingValueKey];
}

- (NSString *) stringValue 
{
	return [self contentStringValue];
}

- (void) setStringValue:(NSString *)string 
{
	[self setContentStringValue:string];
}


@end

// Slugged element definition

static NSString *const slugKey = @"slug";

@implementation OosSluggedElement

+ (id) oosSluggedElementWithString:(NSString *)string 
{
	OosSluggedElement *element = [[[self alloc] init] autorelease];
	[element setStringValue: string];
	return element;
}

- (void) addParseDeclarations 
{
	NSArray *attributes = [NSArray arrayWithObject:slugKey];
	[self addLocalAttributeDeclarations:attributes];
	[self addContentValueDeclaration];
}

- (NSString *) slug 
{
	return [self stringValueForAttribute:slugKey];
}

- (void) setSlug:(NSString *)slug 
{
	[self setStringValue:slug forAttribute:slugKey];
}

- (NSString *) stringValue 
{
	return [self contentStringValue];
}

- (void) setStringValue:(NSString *)string 
{
	[self setContentStringValue:string];
}

@end

// Slugged Elements

@implementation OosSubDependentLocality
+ (NSString *) extensionElementLocalName	{ return @"subdependentlocality"; }
@end

@implementation OosDependentLocality
+ (NSString *) extensionElementLocalName	{ return @"dependentlocality"; }
@end

@implementation OosLocality
+ (NSString *) extensionElementLocalName	{ return @"locality";}
@end

@implementation OosAdministrativeArea
+ (NSString *) extensionElementLocalName	{ return @"administrativearea"; }
@end

@implementation OosSubAdministrativeArea
+ (NSString *) extensionElementLocalName	{ return @"subadministrativearea"; }
@end

@implementation OosCountry
+ (NSString *) extensionElementLocalName	{ return @"country"; }
@end

@implementation OosLeadService
+ (NSString *) extensionElementLocalName	{ return @"lead-service"; }
@end

@implementation OosPrivacyElement
+ (NSString *) extensionElementLocalName	{ return @"privacy"; }
@end

@implementation OosCommaValuesElement

- (NSArray /* NSString */ *)values
{
   return [[self stringValue] componentsSeparatedByString:@","];
}

@end

@implementation OosLists
+ (NSString *) extensionElementLocalName	{ return @"lists"; }

@end

@implementation OosTags
+ (NSString *) extensionElementLocalName	{ return @"tags"; }
@end

#define OOS_LISTS_SCHEME @"http://api.11870.com/api/v2/lists/%@"
#define OOS_TAGS_SCHEME @"http://api.11870.com/api/v2/tags/%@"

@implementation OosCategoryList

+ (OosCategoryList *)listWithTerm:(NSString *)term forUserSlug: (NSString *) userSlug
{
    NSString *scheme = [NSString stringWithFormat:OOS_LISTS_SCHEME, userSlug, nil];
    return (OosCategoryList *) [self categoryWithScheme:scheme term:term];
}

@end

@implementation OosCategoryTag

+ (OosCategoryTag *)tagWithTerm:(NSString *)term forUserSlug:(NSString *)userSlug
{
    NSString *scheme = [NSString stringWithFormat:OOS_TAGS_SCHEME, userSlug, nil];
    return (OosCategoryTag *) [self categoryWithScheme:scheme term:term];
}

@end

@implementation OosTitle

+ (NSString *) extensionElementLocalName    { return @"title"; }

@end

@implementation OosSummary

+ (NSString *)extensionElementLocalName     { return @"summary"; }

@end

@implementation OosOfferType

+ (NSString *)extensionElementLocalName     { return @"offerType"; }

@end

@implementation OosSignups

+ (NSString *)extensionElementLocalName     { return @"signups"; }

@end

@implementation OosValue

+ (NSString *)extensionElementLocalName     { return @"value"; }

@end

